unit UDevCtrlEdit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UDevCtrlAddEditBase, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  dxSkinsCore, dxSkinOffice2016Colorful, Vcl.Menus, Data.DB, MemDS, DBAccess,
  Uni, System.ImageList, Vcl.ImgList, cxImageList, Vcl.StdCtrls, cxButtons,
  Vcl.ExtCtrls, cxMaskEdit, cxDropDownEdit, cxTextEdit, cxLabel;

type
  TfrmDevCtrlEdit = class(TfrmDevCtrlAddEditBase)
    procedure FormShow(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmDevCtrlEdit: TfrmDevCtrlEdit;

implementation

uses
  UCommonFunSqlite, UCommonConst;

{$R *.dfm}

procedure TfrmDevCtrlEdit.btnSaveClick(Sender: TObject);
var
  sSql, sCtrlName, sIP: string;
  iThemeID: Integer;
begin
  inherited;

  sCtrlName := Trim(edtName.Text);
  if sCtrlName.IsEmpty then
  begin
    MsgBoxDefault(RESDEVCTRLNAMEEMPTY);
    edtName.SetFocus;
    Abort;
  end;

  sSql := 'select CtrlName from DevCtrlData where CtrlName=' + QuotedStr(sCtrlName) + ' and CtrlID<>' + QuotedStr(IntToStr(FSelectID));

  if getSingleValueStr(sSql) <> '' then
  begin
    MsgBox(RESDEVCTRLNAMEEXIST);
    edtName.SetFocus;
    Abort;
  end;;

  sIP := Trim(edtIP.Text);
  if sIP.IsEmpty then
  begin
    MsgBoxDefault(RESDEVCTRLIPEMPTY);
    edtIP.SetFocus;
    Abort;
  end;

  if comTheme.ItemIndex = -1 then
  begin
    MsgBoxDefault(RESDEVCTRLTHEMENOSELECTED);
    edtIP.SetFocus;
    Abort;
  end;

  iThemeID := Integer(comTheme.Properties.Items.Objects[comTheme.ItemIndex]);

  sSql := 'update DevCtrlData set CtrlName=' + QuotedStr(sCtrlName)
        + ',CtrlIP=' + QuotedStr(sIP)
        + ',ThemeID=' + QuotedStr(IntToStr(iThemeID))
        + ' where CtrlID=' + QuotedStr(IntToStr(FSelectID));

  if ExecSql(sSql) then
  begin
    SendMessage(FParentHandle, WM_REFRESHDATA, 0, 0);

    MsgBoxDefault(RESSAVESUCCESS);
  end
  else
    MsgBoxDefault(RESSAVEFAIL);

end;

procedure TfrmDevCtrlEdit.FormShow(Sender: TObject);
var
  sSql: string;
  iThemeID: Integer;
begin
  inherited;

  sSql := 'select * from DevCtrlData where CtrlID=''' + IntToStr(FSelectID) + '''';

  with qryTmp do
  begin
    close;
    SQL.Text := sSql;
    Open;

    edtName.Text := Trim(FieldByName('CtrlName').AsString);
    edtIP.Text := Trim(FieldByName('CtrlIP').AsString);
    iThemeID := FieldByName('ThemeID').AsInteger;

    LocationCom(comTheme, iThemeID);
  end;
end;

end.
